package edu.ynu.se.xiecheng.achitectureclass.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.dao.BusinessDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ItemDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.OorderDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.ShopDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Business;
import edu.ynu.se.xiecheng.achitectureclass.entity.Item;
import edu.ynu.se.xiecheng.achitectureclass.entity.Oorder;
import edu.ynu.se.xiecheng.achitectureclass.entity.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service
public class BusinessService extends LogicService<BusinessDao, Business,Long> {
    @Resource
    private ShopDao shopDao;
    @Resource
    private ItemDao itemDao;
    @Resource
    private OorderDao oorderDao;
    public BusinessService(@Autowired BusinessDao bd) {
        super(bd);
    }

    public Business addShop(Long businessId, String shopName){
        Business business = getDAO().getReferenceById(businessId);
        if(shopDao.findByShopName(shopName)==null){
            Shop shop = new Shop();
            shop.setShopName(shopName);
            shop.setBusiness(business);
            shop = shopDao.save(shop);
            business.addShop(shop);
            return getDAO().save(business);
        }
        return business;
    }

    public Set<Oorder> listOrders(Long businessId){
        Business business = getDAO().getReferenceById(businessId);
        List<Shop> shops = shopDao.findAllByBusiness(business);
        Set<Oorder> oorders = new HashSet<>();
        shops.forEach(shop -> {
            oorders.addAll(business.listOrder(shop));
        });
        return oorders;
    }

    public Oorder confirmOrder(Long businessId, Long shopId, Long oorderId){
        Business business = getDAO().getReferenceById(businessId);
        Shop shop = shopDao.getReferenceById(shopId);
        Oorder oorder = oorderDao.getReferenceById(oorderId);
        return oorderDao.save(business.confirmOrder(shop, oorder));

    }

    public Oorder refund(Long businessId, Long shopId, Long oorderId){
        Business business = getDAO().getReferenceById(businessId);
        Shop shop = shopDao.getReferenceById(shopId);
        Oorder oorder = oorderDao.getReferenceById(oorderId);
        return oorderDao.save(business.refund(shop, oorder));

    }

    public Business register(String username, String password){
        Business business = getDAO().findByUsername(username);
        if(business == null){
            business = new Business();
            return getDAO().save(business.register(username, password));
        }
        return business;
    }
    public Business login(String username, String password){
        return getDAO().findByUsernameAndPassword(username, password);
    }

    public Item createItem(String itemName) {
        Item item = itemDao.findByItemName(itemName);
        if( item != null){
            return null;
        }
        item = new Item();
        item.setItemName(itemName);
        return itemDao.save(item);
    }
}
